package com.gusha.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.math.BigInteger;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysAdminDao;
import com.gusha.dao.SysRoleDao;
import com.gusha.dao.SysRoleMenuDao;
import com.gusha.model.SysMenu;
import com.gusha.model.SysRole;
import com.gusha.model.SysRoleMenu;
import com.gusha.utils.JsonResult;


/**
 * @Created：2018-10-26 15:18:16
 * @Author LightSound的代码生成器
 * @Description:SysRoleService服务类
 */

@Service
@Transactional
public class SysRoleService {

    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;
    @Autowired
    private SysAdminDao sysAdminDao;

    public Page<SysRole> findAll(Pageable pageable, SysRole sysRole) {
        @SuppressWarnings("serial")
        Page<SysRole> result = sysRoleDao.findAll(new Specification<SysRole>() {
            @Override
            public Predicate toPredicate(Root<SysRole> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (sysRole.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),sysRole.getId()));
                }
                if (StringUtils.isNotBlank(sysRole.getRoleName())) {
                    list.add(cb.like(root.get("roleName").as(String.class),"%"+sysRole.getRoleName()+"%"));
                }
                if (StringUtils.isNotBlank(sysRole.getRoleDesc())) {
                    list.add(cb.equal(root.get("roleDesc").as(String.class),sysRole.getRoleDesc()));
                }
                if (sysRole.getCreateTime() != null) {
                    list.add(cb.equal(root.get("createTime").as(Date.class),sysRole.getCreateTime()));
                }
                if (sysRole.getUpdateTime() != null) {
                    list.add(cb.equal(root.get("updateTime").as(Date.class),sysRole.getUpdateTime()));
                }
                if (StringUtils.isNotBlank(sysRole.getCreator())) {
                    list.add(cb.equal(root.get("creator").as(String.class),sysRole.getCreator()));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }

	public JsonResult saveRole(SysRole sysRole, String menuIds) {
		JsonResult result = new JsonResult(false);
		result.setMsg("编辑失败");
		List<SysRoleMenu> list = new ArrayList<>();
		if(sysRole.getId()==null) {//添加
			
			sysRole = sysRoleDao.saveAndFlush(sysRole);
			String[] menuIdsStr = menuIds.split("\\,");
			for (int i = 0; i < menuIdsStr.length; i++) {
				SysRoleMenu roleMenu = new SysRoleMenu();
				roleMenu.setRole(sysRole);
				SysMenu menu = new SysMenu();
				menu.setId(new BigInteger(menuIdsStr[i]));
				roleMenu.setMenu(menu);
				list.add(roleMenu);
			}
		}
		else {//编辑
			//删除所有角色菜单
			sysRole = sysRoleDao.saveAndFlush(sysRole);
			sysRoleMenuDao.deleteAllByRole(sysRole);
			
			String[] menuIdsStr = menuIds.split("\\,");
			for (int i = 0; i < menuIdsStr.length; i++) {
				SysRoleMenu roleMenu = new SysRoleMenu();
				roleMenu.setRole(sysRole);
				SysMenu menu = new SysMenu();
				menu.setId(new BigInteger(menuIdsStr[i]));
				roleMenu.setMenu(menu);
				list.add(roleMenu);
			}
		}
		if(list.size()>0) {
			sysRoleMenuDao.saveAll(list);
		}
		result.setFlag(true);
		result.setMsg("编辑成功");
		return result;
	}

	public JsonResult deleteRole(BigInteger id) {
		JsonResult result = new JsonResult(false);
		result.setMsg("删除失败");
		SysRole role = sysRoleDao.getOne(id);
		sysRoleMenuDao.deleteAllByRole(role);
		sysAdminDao.deleteAllByRoleId(id);
		sysRoleDao.delete(role);
		result.setFlag(true);
		result.setMsg("删除成功");
		return result;
	}

}
